﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using SerbinAV.MobileSales.Sales.Common;
using SerbinAV.MobileSales.Sales.Entities;
using SerbinAV.MobileSales.Sales.Views;


namespace SerbinAV.MobileSales.Sales.Presenters
{
    public class SettingsPresenter : IPresenter
    {
        private IWork _work;
        private ISettingsView _view;
        private Settings _settings;

        public SettingsPresenter(IWork work, ISettingsView view)
        {
            _work = work;
            _view = view;
            _settings = _work.GetSettings();
            _view.SaveChanges += SaveSettings;
            _view.CancelChanges += CancelChanges;
           // _view.TestGps += TestGps;
        }

        public void SaveSettings(object sender, EventArgs e)
        {
            try
            {
                _settings.AgentName = _view.AgentName;
                _settings.CompanyName = _view.CompanyName;

                _settings.DatabasePath = _view.DbPath;
                _settings.DatabasePassword = _view.DbPassword;
                _settings.ImportPath = _view.ImportPath;
                _settings.ExportPath = _view.ExportPath;
                _settings.IsSaveFiles = _view.IsSaveFile;
                _settings.VisibleNullGoods = _view.VisibleNullGoods;
                _settings.IsVanSales = _view.IsVanSales;

                _settings.FtpUrl = _view.FtpUrl;
                _settings.FtpPort = _view.FtpPort;
                _settings.FtpLogin = _view.FtpLogin;
                _settings.FtpPassword = _view.FtpPassword;
                _settings.FtpIsPassivMode = _view.FtpIsPasiveMode;
                _settings.FtpImportPath = _view.FtpImportPath;
                _settings.FtpExportPath = _view.FtpExportPath;

                _settings.DefaultSklad = _view.CurrentSklad;
                _settings.CheckRemainingGoods = _view.CheckRemainingGoods;
                _settings.AutoGenerateNumber = _view.AutoGenerateNumber;
                _settings.NumberDocument = _view.NumberDocument;
                _settings.EnableGPS = _view.EnableGps;

                _settings.PrintPath = _view.PrintPath;
                _settings.ExecuteProgramPrint = _view.ExecuteProgramPrint;

                _work.SaveSettings();
            }
            catch (Exception ex)
            {
                _work.HandlerException.Go("Неможливо зберегти настройки", "Помилка при збереженны настройок", ex);
            }
        }

        public void CancelChanges (object sender, EventArgs e)
        {
            _settings = _work.GetSettings();
        }

        //public void TestGps(object sender, EventArgs e)
        //{
        //    GpsWorker.GetInstance().StartGps();
        //    _view.GpsTestData = GpsWorker.GetInstance().GpsData.ToString();
        //}

        public bool Go()
        {
            _view.CompanyName = _settings.CompanyName;
            _view.AgentName = _settings.AgentName;

            _view.DbPassword = string.IsNullOrEmpty(_settings.DatabasePassword) ? string.Empty : _settings.DatabasePassword;
            _view.DbPath = string.IsNullOrEmpty(_settings.DatabasePath) ? string.Empty : _settings.DatabasePath;
            _view.ExportPath = string.IsNullOrEmpty(_settings.ExportPath) ? string.Empty : _settings.ExportPath;
            _view.ImportPath = string.IsNullOrEmpty(_settings.ImportPath) ? string.Empty : _settings.ImportPath;
            _view.IsSaveFile = _settings.IsSaveFiles;
            _view.CheckRemainingGoods = _settings.CheckRemainingGoods;
            _view.VisibleNullGoods = _settings.VisibleNullGoods;
            _view.IsVanSales = _settings.IsVanSales;

            _view.FtpUrl = string.IsNullOrEmpty(_settings.FtpUrl) ? string.Empty : _settings.FtpUrl;
            _view.FtpPort = _settings.FtpPort;
            _view.FtpLogin = string.IsNullOrEmpty(_settings.FtpUrl) ? string.Empty : _settings.FtpLogin;
            _view.FtpPassword = string.IsNullOrEmpty(_settings.FtpUrl) ? string.Empty : _settings.FtpPassword;
            _view.FtpIsPasiveMode = _settings.FtpIsPassivMode;
            _view.FtpImportPath = string.IsNullOrEmpty(_settings.FtpImportPath) ? string.Empty : _settings.FtpImportPath;
            _view.FtpExportPath = string.IsNullOrEmpty(_settings.FtpExportPath) ? string.Empty : _settings.FtpExportPath;

            try
            {
                _view.sklads = _work.DataManager != null ? _work.DataManager.Sklads.GetAll() : new List<Sklad>();
            }
            catch (Exception ex)
            {
                _work.HandlerException.Go("Помилка при зєднанні з БД", "Завантаження настройок", ex);
            }
            
            _view.CurrentSklad = _settings.DefaultSklad;
            _view.AutoGenerateNumber = _settings.AutoGenerateNumber;
            _view.NumberDocument = _settings.NumberDocument;

            _view.EnableGps = _settings.EnableGPS;

            _view.PrintPath = _settings.PrintPath;
            _view.ExecuteProgramPrint = _settings.ExecuteProgramPrint;
            return _view.Show();
        }
    }
}
